package com.example.mypigcar.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mypigcar.mapper.AppointmentDao;
import com.example.mypigcar.mapper.CarDao;
import com.example.mypigcar.pojo.dto.AddAppointmentDTO;
import com.example.mypigcar.pojo.po.Appointment;
import com.example.mypigcar.pojo.po.Car;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zkw
 * @Description TODO
 * @createTime 2021年07月07日 08:45:00
 */
@Service
@Transactional
public class AppointmentServiceImpl implements AppointmentService {
    @Resource
    AppointmentDao appointmentDao;

    @Resource
    CarDao carDao;

    /**
     * 添加预约单
     * @param addAppointMentDto 添加预约单DTO
     * @param userId 用户id
     * @Description 1. 查询用户当前是否有预约，有预约不能再预约
     *              2. 更新车辆预约状态 为 1可预约
     * @return boolean
     */
    @Override
    public boolean insertAppointment(AddAppointmentDTO addAppointMentDto,Integer userId) throws Exception {

        //1.查看用户是否有预约
        QueryWrapper<Appointment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.ne("status", 2);
        List<Appointment> appointments = appointmentDao.selectList(queryWrapper);
        if (appointments.size()>0){
            throw new Exception("一次只能预约一辆车嗷");
        }

        //2.更新车辆预约状态
        QueryWrapper<Car> cq = new QueryWrapper<>();
        cq.eq("car_id", addAppointMentDto.getCarId());
        Car car = new Car();
        car.setStatus(1);
        carDao.update(car, cq);


        Appointment appointment = new Appointment();
        BeanUtils.copyProperties(addAppointMentDto, appointment);
        appointment.setCreateTime(new Date());
        appointment.setStatus(0);
        appointment.setUserId(userId);
        int insert = appointmentDao.insert(appointment);
        return insert>0;
    }

    /**
     * 获取用户的全部预约单
     * @param userId 用户id
     * @return 预约单list
     */
    @Override
    public List<Appointment> getUserAppointment(int userId) {
        QueryWrapper<Appointment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return appointmentDao.selectList(queryWrapper);
    }

    /**
     * 更新预约单根据用户id和预约单id
     * @param appointment 预约单
     * @return boolean
     */
    @Override
    @Transactional
    public boolean updateByUserIdAndAppointmentId(Appointment appointment) throws Exception {
        QueryWrapper<Appointment> aq = new QueryWrapper<>();
        aq.eq("user_id", appointment.getUserId());
        aq.eq("appointment_id", appointment.getAppointmentId());
        int update = appointmentDao.update(appointment, aq);

        //更新车辆状态
        Appointment selectOne = appointmentDao.selectOne(aq);
        Integer carId = selectOne.getCarId();
        QueryWrapper<Car> cq = new QueryWrapper<>();
        cq.eq("car_id", carId);
        Car car = new Car();
        //如果要更新的状态是0已预约，就修改车辆状态
        if(appointment.getStatus()==0 || appointment.getStatus()==1){
            car.setStatus(1);
            carDao.update(car, cq);
        }else if (appointment.getStatus()==2){
            car.setStatus(0);
            carDao.update(car, cq);
        }
        return update>0;
    }
}
